package com.example.index.system

import android.app.Dialog
import android.os.Build
import android.os.Bundle
import android.view.*
import android.view.accessibility.AccessibilityEvent
import androidx.appcompat.app.AppCompatActivity
import com.example.index.R

class PhoneWindowScrollingActivity : AppCompatActivity() {


    lateinit var dialogCallBackDelegate: Window.Callback

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_phone_window_scrolling)

        dialogCallBackDelegate = window.callback

        val windowMask = Dialog(this)
        windowMask.window?.callback = object : Window.Callback {

            override fun onActionModeFinished(mode: ActionMode?) {
                window.callback?.onActionModeFinished(mode)
            }

            override fun onCreatePanelView(featureId: Int): View? {
                return dialogCallBackDelegate.onCreatePanelView(featureId)
            }

            override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
                return delegateDispatchTouchEvent(event)

            }

            override fun onCreatePanelMenu(featureId: Int, menu: Menu?): Boolean {
                return dialogCallBackDelegate.onCreatePanelMenu(featureId, menu)
            }

            override fun onWindowStartingActionMode(callback: ActionMode.Callback?): ActionMode? {

                return dialogCallBackDelegate.onWindowStartingActionMode(callback)
            }

            override fun onWindowStartingActionMode(callback: ActionMode.Callback?, type: Int): ActionMode? {

                return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    dialogCallBackDelegate.onWindowStartingActionMode(callback, type)
                } else {
                    null
                }
            }

            override fun onAttachedToWindow() {

                dialogCallBackDelegate.onAttachedToWindow()
            }

            override fun dispatchGenericMotionEvent(event: MotionEvent?): Boolean {
                return dialogCallBackDelegate.dispatchGenericMotionEvent(event)
            }

            override fun dispatchPopulateAccessibilityEvent(event: AccessibilityEvent?): Boolean {

                return dialogCallBackDelegate.dispatchPopulateAccessibilityEvent(event)
            }

            override fun dispatchTrackballEvent(event: MotionEvent?): Boolean {
                return dialogCallBackDelegate.dispatchTrackballEvent(event)
            }

            override fun dispatchKeyShortcutEvent(event: KeyEvent?): Boolean {

                return dialogCallBackDelegate.dispatchKeyShortcutEvent(event)
            }

            override fun dispatchKeyEvent(event: KeyEvent?): Boolean {

                return dialogCallBackDelegate.dispatchKeyEvent(event)
            }

            override fun onMenuOpened(featureId: Int, menu: Menu?): Boolean {
                return dialogCallBackDelegate.onMenuOpened(featureId, menu)
            }

            override fun onPanelClosed(featureId: Int, menu: Menu?) {
                dialogCallBackDelegate.onPanelClosed(featureId, menu)
            }

            override fun onMenuItemSelected(featureId: Int, item: MenuItem?): Boolean {
                return dialogCallBackDelegate.onMenuItemSelected(featureId, item)
            }

            override fun onDetachedFromWindow() {
                dialogCallBackDelegate.onDetachedFromWindow()
            }

            override fun onPreparePanel(featureId: Int, view: View?, menu: Menu?): Boolean {

                return dialogCallBackDelegate.onPreparePanel(featureId, view, menu)
            }

            override fun onWindowAttributesChanged(attrs: WindowManager.LayoutParams?) {
                dialogCallBackDelegate.onWindowAttributesChanged(attrs)
            }

            override fun onWindowFocusChanged(hasFocus: Boolean) {
                dialogCallBackDelegate.onWindowFocusChanged(hasFocus)
            }

            override fun onContentChanged() {
                dialogCallBackDelegate.onContentChanged()
            }

            override fun onSearchRequested(): Boolean {
                return dialogCallBackDelegate.onSearchRequested()
            }

            override fun onSearchRequested(searchEvent: SearchEvent?): Boolean {
                return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    dialogCallBackDelegate.onSearchRequested(searchEvent)
                } else {
                    false
                }
            }

            override fun onActionModeStarted(mode: ActionMode?) {

                dialogCallBackDelegate.onActionModeStarted(mode)
            }

        }
    }

    private fun delegateDispatchTouchEvent(event: MotionEvent?): Boolean {

        //这里需要计算 toolbar 、statusbar 的高度，只有内容部分 的触摸事件需要被代理

        return dialogCallBackDelegate.dispatchTouchEvent(event)
    }
}
